package com.haogu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haogu.entity.Contacts;
import com.haogu.entity.CustomerList;
import com.haogu.entity.VContacts;
import com.haogu.mapper.ContactsMapper;
import com.haogu.service.ContactsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ContactsServiceImpl implements ContactsService {
    @Resource
    private ContactsMapper contactsMapper;

    @Resource
    private JavaMailSender javaMailSender;


    @Override
    public Contacts selectContacts(Integer id) {
        return contactsMapper.selectContacts(id);
//        return contactsMapper.selectById(id);
    }

    @Override
    public List<Contacts> selectContactsList(Integer id) {
        QueryWrapper<Contacts> oq = new QueryWrapper<>();
        oq.eq("customer_id", id);//判断id是否相等
        return contactsMapper.selectList(oq);
    }

    /**
     * 查询所有联系人
     * @return
     */
    @Override
    public List<VContacts> selectContactsLists(Integer page,Integer size) {
        return contactsMapper.selectContactsLists((page - 1) * size, size);
    }
    /**
     * 查询联系人总数
     * @return
     */
    @Override
    public int rows() {
        return contactsMapper.selectCount(null);
    }

    /**
     * 今日、周、月、最近新建查询
     *
     * @return
     */
    @Override
    public List<VContacts> todayNewCreateSelect(String type) {
        List<VContacts> customerLists = null;
        if ("day".equals(type) || "week".equals(type) || "month".equals(type)) {
            customerLists = contactsMapper.todayNewCreateSelect(type);
        }
        return customerLists;
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @Override
    public int delCustomers(Integer id) {
        int i = contactsMapper.deleteById(id);
        return i;
    }

    /**
     * 填充删除时间及删除者的id
     *
     * @param id
     */
    @Override
    public void deleteTime(Integer deleteId, Integer id) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        contactsMapper.deleteTime(deleteId, format, id);
    }

    /**
     * 根据条件模糊查询
     *
     * @param search
     * @return
     */
    @Override
    public List<VContacts> selectByCustomer(String search, String filterSearch) {
        Integer va = Integer.valueOf(filterSearch);
        List<VContacts> listList = null;
        if (va == 0 || va == 1 || va == 2 || va == 3) {
            listList = contactsMapper.selectCustomerLike(search, filterSearch);
        } else {
            return null;
        }
        return listList;
    }

    /**
     * 邮件发送
     *
     * @param from    发送者
     * @param to      接收者
     * @param subject 邮件标题
     * @param content 邮件内容
     */
    public void sendSimpleMail(String from, String to, String subject, String content) {
        Logger logger = LoggerFactory.getLogger(this.getClass());
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);//发送者
        System.out.println();
        message.setTo(to);//接收者
        message.setSubject(subject);//邮件标题
        message.setText(content);//邮件内容

        try {
            javaMailSender.send(message);
            logger.info("简单邮件已经发送。");
        } catch (Exception e) {
            logger.error("发送简单邮件时发生异常！", e);
        }
    }

    /**
     * 查询联系人某一条信息
     * @param id
     * @return
     */
    @Override
    public Contacts selectCustomer(Integer id) {
        return contactsMapper.selectById(id);
    }

    @Override
    public void change(Contacts contacts) {
        contactsMapper.updateById(contacts);
    }

    /*@Override
    public void change(Contacts contacts) {
        contactsMapper.updateById(contacts);
    }*/
}
